package net.hn.hnms.biz.indicator.util;

import cn.hutool.core.util.StrUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author 智呈物联 nzw
 * @date 2022/8/24
 */
public class IndicatorUtil {

    public static final Double doubleDefault=null;
    public static final String strDefault=null;
    public static final BigDecimal bigDecimalDefault=null;
    private static String requestUrl;

    public static String levelName(Double value){
        if(null==value){
            return "蓝色风险";
        }
        if(value<60){
            return "红色风险";
        }
        if(60<=value&&value<75){
            return "橙色风险";
        }
        if(75<=value&&value<85){
            return "黄色风险";
        }
        return "蓝色风险";
    }

    public static String level(Double value){
        if(value<60){
            return "一";
        }
        if(60<=value&&value<75){
            return "二";
        }
        if(75<=value&&value<85){
            return "三";
        }
        return "四";
    }

    public static String alarmTrend(int value){
        if(value==0){
            return "不变";
        }
        if(value>0){
            return "增加";
        }
        return "减少";
    }

    public static String value0Reserve(String value){
        BigDecimal bigDecimal = new BigDecimal(value);
        BigDecimal bigDecimal1 = bigDecimal.setScale(0, BigDecimal.ROUND_HALF_UP);
        return bigDecimal1.toString();
    }

    /**
     * 计算值 并保留2位
     * @param value
     * @return
     */
    public static String valueCompute(String value){
        if(StrUtil.isBlank(value)){
            return value;
        }
        double d=Double.parseDouble(value);
        BigDecimal big=new BigDecimal(d);
        big=big.setScale(2, BigDecimal.ROUND_HALF_UP);
        return big.stripTrailingZeros().toPlainString();
    }

    public static String getUrl(){
        if(StrUtil.isBlank(requestUrl)){
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if(Objects.nonNull(requestAttributes)&&requestAttributes instanceof ServletRequestAttributes){
                HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
                StringBuffer url = request.getRequestURL();
                String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
                if(!tempContextUrl.endsWith("/")){
                    tempContextUrl=tempContextUrl+"/";
                }
                requestUrl=tempContextUrl;
            }
        }
        return requestUrl;
    }
    /**
     * 获取水害的一级code
     * @return
     */
    public static String getFloodCode(){
        return "10";
    }

    /**
     * 获取火灾的一级code
     * @return
     */
    public static String getFireCode(){
        return "20";
    }

    /**
     * 获取瓦斯的一级code
     * @return
     */
    public static String getGasCode(){
        return "30";
    }

    /**
     * 获取粉尘的一级code
     * @return
     */
    public static String getDustCode(){
        return "40";
    }

    /**
     * 获取顶板的一级code
     * @return
     */
    public static String getRoofCode(){
        return "";
    }

    /**
     * 获取内因二级code
     * @return
     */
    public static String inFireSecondCode(){
        return "2002";
    }

    /**
     * 获取外因二级code
     * @return
     */
    public static String outFireSecondCode(){
        return "2003";
    }

    /**
     * 获取火灾安全管理二级code
     * @return
     */
    public static String fireSafeSecondCode(){
        return "2004";
    }

    /**
     * 获取火灾人员素质二级code
     * @return
     */
    public static String firePersonSecondCode(){
        return "2001";
    }
}
